<template>
  <el-container class="home">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.gif" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isOpen ? '64px' : '200px'">
        <!-- 折叠按钮 -->
        <div class="collapse" @click="toggleMenu">|||</div>
        <!-- 导航 -->
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#007ACC"
          router
          unique-opened
          :collapse="isOpen"
          :collapse-transition="false"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="'/' + item.path" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 菜单图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 菜单名称 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 菜单项 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="changeActivePath('/' + subItem.path)">
              <template>
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
            <!-- 二级菜单:我们不需要二级菜单,只需要菜单项即可 -->
            <!-- <el-submenu index="1-4">
              <template slot="title">选项4</template>
              <el-menu-item index="1-4-1">选项1</el-menu-item>
            </el-submenu> -->
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main>
        <div class="main-cotent">
          <router-view></router-view>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      activePath: '', //当前激活的index,控制菜单高亮
      isOpen: false,
      menuList: [],
      iconObj: {
        // eslint要求键必能加引号
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 动态改变菜单的高亮显示状态
    changeActivePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 获取菜单数据
    async getMenuList () {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message(res.meta.msg)
      this.menuList = res.data
    },
    // 折叠展开菜单
    toggleMenu () {
      this.isOpen = !this.isOpen
    },
    // 退出登录
    logout () {
      // this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   this.$message({
      //     type: 'success',
      //     message: '删除成功!'
      //   })
      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消删除'
      //   })
      // })
      this.$showMessageBox('确定退出吗?', () => {
        // 登出就是清除token,并跳转到登录页面
        // window.sessionStorage.clear()
        // window.sessionStorage.removeItem('token')
        window.sessionStorage.clear()
        this.$router.push('/login')
        this.$message.success('成功退出')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #363d40;
  padding-left: 0;
  font-size: 20px;
  color: #fff;
  > div {
    display: flex;
    align-items: center;
    height: 100%;
    > img {
      height: 70%;
      border-radius: 50%;
      background-color: #fff;
      box-shadow: 0 0 3px #ccc;
      margin-right: 10px;
    }
  }
}
.el-aside {
  background-color: #313743;
  .collapse{
    display: flex;
    align-items: center;
    justify-content: center;
    height: 26px;
    background-color: #475163;
    font-size: 16px;
    color: #fff;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-menu{
    border-right: none; // 重置样式
    .iconfont{
      margin-right: 10px;
    }
  }
}
.el-main {
  background-color: #e9edf1;
  padding: 0;
  background-image: url(../assets/home-bg.jpg);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-origin: content-box;
  background-position: center;
  .main-cotent{
    padding: 20px;
  }
}
</style>
